/*-----------------------------------------------------------------------------
*
*	Copyright (C) 2005 ALI Corp. All rights reserved.
*
*	File: obj_scrollbar.c
*
*	Content: 
*		scrollbar  object
*	History: 
*		2005/12/30 by benjamin
*		Created this file. Inherited from new osd lib
*-----------------------------------------------------------------------------*/
#include <sys_config.h>

#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libosd/osd_common_draw.h>
#include "osd_lib_internal.h"

#define BAR_PRINTF(...)


static void OSD_ScrollBarGetMidRect(PSCROLL_BAR b,OSD_RECT* MidRect)
{
	MidRect->uLeft  =b->head.frame.uLeft+b->rcBar.uLeft;
	MidRect->uTop=b->head.frame.uTop+b->rcBar.uTop;
	MidRect->uWidth=b->rcBar.uWidth;
	MidRect->uHeight=b->rcBar.uHeight;

    if(!(b->bStyle & BAR_THUMB_RECT_SUPPORT_OVERFLOW))
    {
    	if(MidRect->uLeft>b->head.frame.uLeft+b->head.frame.uWidth)
    		MidRect->uLeft =b->head.frame.uLeft+b->head.frame.uWidth;
    	if(MidRect->uTop>b->head.frame.uTop+b->head.frame.uHeight)
    		MidRect->uTop =b->head.frame.uTop+b->head.frame.uHeight;
    	if(MidRect->uLeft+MidRect->uWidth>b->head.frame.uLeft+b->head.frame.uWidth)
    		MidRect->uWidth =b->head.frame.uLeft+b->head.frame.uWidth-MidRect->uLeft;
    	if(MidRect->uTop+MidRect->uHeight>b->head.frame.uTop+b->head.frame.uHeight)
    		MidRect->uHeight =b->head.frame.uTop+b->head.frame.uHeight-MidRect->uTop;
    }
}


void GetThumbRect(PSCROLL_BAR b, POSD_RECT pRect)
{
	UINT32 orient;
	OSD_RECT mid_rect;
	UINT16 bw,bh;
	PWINSTYLE  	lpWinSty;
	UINT16 minLoca, maxLoca, minThumbSize,start_pos,end_pos,page_width;

	OSD_ScrollBarGetMidRect(b,&mid_rect);

	if ( (b->bStyle & BAR_HORI_AUTO) || (b->bStyle & BAR_HORI_ORIGINAL))
	{
		minLoca = mid_rect.uLeft;
		maxLoca = mid_rect.uLeft + mid_rect.uWidth;
		orient = 0;
	}
	else// if ( (b->bStyle & BAR_VERT_AUTO) || (b->bStyle & BAR_VERT_ORIGINAL) )
	{
		minLoca = mid_rect.uTop;
		maxLoca = mid_rect.uTop + mid_rect.uHeight;
		orient = 1;
	}

	if(OSD_GetLibInfoByWordIdx(b->wThumbID, LIB_ICON, &bw, &bh))
	{
		//icon
		if(orient == 0)
			minThumbSize = bw;
		else
			minThumbSize = bh;
	}
	else
	{
		lpWinSty = g_osd_rsc_info.osd_get_win_style(b->wThumbID);
		minThumbSize = 0;
        
		if(SST_GET_STYLE(lpWinSty->bWinStyleType) != C_WS_PIC_DRAW)
			minThumbSize = 2;
		else
		{
			if(orient == 0)
			{
                if(OSD_GetLibInfoByWordIdx(lpWinSty->wLeftTopIdx, lpWinSty->IconLibIdx, &bw, &bh))
                	minThumbSize += bw;
                else if(OSD_GetLibInfoByWordIdx(lpWinSty->wLeftLineIdx, lpWinSty->IconLibIdx, &bw, &bh))
                	minThumbSize += bw;

                if(OSD_GetLibInfoByWordIdx(lpWinSty->wRightTopIdx, lpWinSty->IconLibIdx, &bw, &bh))
                	minThumbSize += bw;
                else if(OSD_GetLibInfoByWordIdx(lpWinSty->wRightLineIdx, lpWinSty->IconLibIdx, &bw, &bh))
                	minThumbSize += bw;
			}
			else
			{
				if(OSD_GetLibInfoByWordIdx(lpWinSty->wLeftTopIdx, lpWinSty->IconLibIdx, &bw, &bh))
					minThumbSize += bh;
				else if(OSD_GetLibInfoByWordIdx(lpWinSty->wTopLineIdx, lpWinSty->IconLibIdx, &bw, &bh))
					minThumbSize += bh;

				if(OSD_GetLibInfoByWordIdx(lpWinSty->wLeftButtomIdx, lpWinSty->IconLibIdx, &bw, &bh))
					minThumbSize += bh;
				else if(OSD_GetLibInfoByWordIdx(lpWinSty->wBottomLineIdx, lpWinSty->IconLibIdx, &bw, &bh))
					minThumbSize += bh;
			}

            if(minThumbSize == 0)
                minThumbSize = 2;
		}
	}

	if(b->bStyle&BAR_SCROLL_COMPLETE)
	{
		if(b->bPage< b->nMax )
		{
			page_width = (maxLoca  - minLoca+ 1)*b->bPage/b->nMax;
			if(page_width<minThumbSize)
				page_width = minThumbSize;
			if(b->nMax>1)
				start_pos = minLoca + (maxLoca - minLoca-minThumbSize)*(b->nPos)/(b->nMax-1);
			else
				start_pos = minLoca;
			end_pos = start_pos + page_width;
		}
		else
		{
			start_pos = minLoca;
			end_pos = minLoca;
		}
	}
	else
	{
		if(b->bPage == 0)
		{
			if(b->nMax > 0)
				start_pos = minLoca + b->nPos * (maxLoca - minLoca) / b->nMax;
			else
				start_pos = minLoca;
			end_pos = minLoca + minThumbSize;		
		}
	 	else if(b->bPage< b->nMax )
		{
			page_width = (maxLoca  - minLoca+ 1)*b->bPage/b->nMax;
			if(page_width<minThumbSize)
				page_width = minThumbSize;
			//else
			//    page_width -= minThumbSize;
			if( (b->nMax - b->nPos) <  b->bPage )
				start_pos = maxLoca - page_width;
			else
				start_pos = minLoca + (maxLoca - minLoca  + 1 - page_width)*b->nPos/ (b->nMax -  b->bPage);
			end_pos = start_pos + page_width;
		}
		else
		{
			start_pos   = minLoca;
			end_pos     = maxLoca;
		}
	}

	if(orient == 0)
	{
		pRect->uLeft 	= start_pos;
		pRect->uTop  	= mid_rect.uTop;
		pRect->uWidth 	= end_pos - start_pos + 1;
        if(pRect->uWidth > minThumbSize && b->bStyle & BAR_HORI_ORIGINAL)
            pRect->uWidth = minThumbSize;
		pRect->uHeight 	= mid_rect.uHeight;		
	}
	else
	{
		pRect->uLeft 	= mid_rect.uLeft;
		pRect->uTop  	= start_pos;
		pRect->uWidth 	= mid_rect.uWidth;
		pRect->uHeight 	= end_pos - start_pos + 1;
        if(pRect->uHeight > minThumbSize && b->bStyle & BAR_VERT_ORIGINAL)
            pRect->uHeight = minThumbSize;
	}
}


void OSD_DrawScrollBarCell(PSCROLL_BAR b,UINT8 bStyleIdx,UINT32 nCmdDraw)
{
	
	lpVSCR		pVscr;
	UINT32 style_param;
	OSD_RECT rcThumb,MidRect;
	UINT16 BitmapWith,BitmapHeight;
	BOOL fDrawThumb;
	PWINSTYLE  	lpWinSty;
	UINT32		fgColor;
	lpWinSty 	= g_osd_rsc_info.osd_get_win_style(bStyleIdx);
	fgColor		= lpWinSty->wFgIdx;
	pVscr=OSD_DrawObjectFrame(&b->head.frame, bStyleIdx);

	OSD_ScrollBarGetMidRect(b,&MidRect);
	
	if(OSD_GetLibInfoByWordIdx(b->wTickBg, LIB_ICON, &BitmapWith, &BitmapHeight))//get mid rect ,check if icon
		OSDDrawPicSolidRectangle(pVscr, &MidRect, b->wTickBg, LIB_ICON,lpWinSty->wBgIdx);
	else
		OSD_DrawStyleRect(&MidRect,b->wTickBg,pVscr);
	
	GetThumbRect(b, &rcThumb);
	if(OSD_GetLibInfoByWordIdx(b->wThumbID,LIB_ICON, &BitmapWith,&BitmapHeight))
		OSDDrawPicSolidRectangle(pVscr, &rcThumb, b->wThumbID, LIB_ICON,fgColor);
	else
		OSD_DrawStyleRect(&rcThumb, b->wThumbID, pVscr);
}

VACTION OSD_ScrollBarKeyMap(POBJECT_HEAD pObj, UINT32 vkey)
{
	VACTION Action = VACT_PASS;
	
	return Action;
}

PRESULT OSD_ScrollBarProc(POBJECT_HEAD pObj, UINT32 msg_type,UINT32 msg,UINT32 param1)
{
	PRESULT Result;

	if(msg_type==MSG_TYPE_KEY)
	{
		VACTION Action;
	    	UINT32 vkey;
	    	BOOL bContinue;
    	
		Result = OSD_ObjCommonProc(pObj,msg,param1,&vkey,&Action,&bContinue);
		if(!bContinue)
	    	goto CHECK_LEAVE;
	    	
		switch(Action)
	    {	
    	case VACT_CLOSE:
CLOSE_OBJECT:    		
    		Result = OSD_ObjClose(pObj,C_CLOSE_CLRBACK_FLG);
    		goto EXIT;
    		break;
        default:
            Result = OSD_SIGNAL(pObj, EVN_UNKNOWN_ACTION, (Action<<16) | vkey, param1);
            break;
	    }
	}
	else// if(msg_type==MSG_TYPE_EVNT)
		Result = OSD_SIGNAL(pObj, msg_type, msg, param1);
		
CHECK_LEAVE:
	if(!(EVN_PRE_OPEN == msg_type))
		CHECK_LEAVE_RETURN(Result,pObj);
	
EXIT:			
	return Result;
}
